package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.qhzw.vip.qiyu.entity.DtsUser;
import net.qhzw.vip.qiyu.entity.RepairCode;
import net.qhzw.vip.qiyu.entity.SysUser;
import net.qhzw.vip.qiyu.mapper.RepairCodeMapper;
import net.qhzw.vip.qiyu.model.RepairCodeVo;
import net.qhzw.vip.qiyu.service.DtsUserService;
import net.qhzw.vip.qiyu.service.RepairCodeService;
import net.qhzw.vip.qiyu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import net.qhzw.vip.qiyu.util.SecurityUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
public class RepairCodeServiceImpl extends ServiceImpl<RepairCodeMapper, RepairCode>
        implements RepairCodeService {

    @Autowired
    private RepairCodeMapper repairCodeMapper;

    @Autowired
    private DtsUserService dtsUserService;

    @Autowired
    private SysUserService sysUserService;


    /***
     * 分页查询电动车
     * @param page
     * @return
     */
    @Override
    public PageInfo<RepairCodeVo> findByPage(Page<RepairCode> page, String vehicleNumber, Byte vehicleStatus, Long shopId) {
        Long userId = SecurityUtils.getUserId();
        SysUser user = sysUserService.getById(userId);
        if (user == null || user.getCommunityId() == null) {
            return new PageInfo<>();
        }
        Long communityId = null;
        if (user.getCommunityId() != 1L) {
            communityId = user.getCommunityId();
        }
        PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
        List<RepairCodeVo> repairCodeVoList = repairCodeMapper.findByPage(vehicleNumber, vehicleStatus, communityId, shopId);
        PageInfo<RepairCodeVo> pageInfo = new PageInfo<>(repairCodeVoList);
        return pageInfo;
    }


    @Override
    public boolean addRepairCode(RepairCode repairCode) {

        repairCode.setId(IdWorker.getId());
        repairCode.setStatus((byte) 1);
        repairCode.setCreated(new Date());
        repairCode.setCreateBy(SecurityUtils.getUserId());
        return save(repairCode);
    }

    @Override
    public boolean updateRepairCode(RepairCode repairCode) {
        repairCode.setCreateBy(SecurityUtils.getUserId());
        return updateById(repairCode);
    }

    @Override
    public int updateByPrimaryKeySelective(RepairCode repairCodeBef) {
        return repairCodeMapper.updateByPrimaryKeySelective(repairCodeBef);
    }


    /***
     * 根据当前登录用户分页查询电动车
     * @param page
     * @param userId
     * @return
     */
    @Override
    public Page<RepairCode> findPageByUser(Page<RepairCode> page, Long userId, String vehicleNumber, Long shopId) {
        DtsUser dtsUser = dtsUserService.getById(userId);
        Page<RepairCode> repairCodePage = page(page,
                new LambdaQueryWrapper<RepairCode>()
                        .eq(shopId != 1, RepairCode::getShopId, shopId)
                        .eq(RepairCode::getVehicleBelongs, dtsUser.getCommunityId())
                        .eq(RepairCode::getVehicleStatus, 2)
                        .and(!StringUtils.isEmpty(vehicleNumber), wq -> wq.eq(RepairCode::getVehicleNumber, vehicleNumber)
                                .or()
                                .eq(RepairCode::getFrameNumber, vehicleNumber)
                                .or()
                                .eq(RepairCode::getNumberPlate, vehicleNumber)));
        return repairCodePage;
    }

    @Override
    public void updateByUser(RepairCode repairCodeBef) {
        repairCodeMapper.updateByUser(repairCodeBef);
    }

    @Override
    public boolean updateVehicleStatus(RepairCode repairCode) {
        return repairCodeMapper.updateVehicleStatus(repairCode);

    }

    @Override
    public int closeRepairCode(Long id) {
        return repairCodeMapper.closeRepairCode(id);
    }


    @Override
    public RepairCode getById(Serializable id) {
        return getOne(new LambdaQueryWrapper<RepairCode>()
                .eq(id != null, RepairCode::getId, id)
                .eq(RepairCode::getStatus, 1));
    }
}
